package com.senthink.gl.business.fault;

import com.senthink.gl.business.common.exception.BusinessException;
import com.senthink.gl.business.messages.CodeMsg;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 设备类型枚举定义
 *
 * @author yuanpeng@senthink.com
 * @data 2017/11/30
 */
@Getter
public enum DeviceTypeEnum {

    DEFAULT(0, "默认类型"),

    ZIGBEE_GATEWAY(1, "GL-L1型网关"),

    LAMP(2, "灯具"),

    CHARGING_PILE(3, "充电桩"),

    HAZE_CLEANER(4, "除霾器"),

    ADVERTISING_SCREEN(5, "广告屏"),

    WEBCAM(6, "摄像头"),

    SENSOR(7, "传感设备"),

    NET_COLLECTOR(8, "网络采集器"),

    ALARM(9, "报警器"),

    GEOMAGNETIC_SENSOR(10, "地磁传感器"),

    GEOMAGNETIC_RECEIVER(11, "地磁接收器"),

    RFID_READER(12, "RFID读卡器"),

    RFID_CARD(13, "RFID卡"),

    LIGHT_POLE(14, "灯杆"),

    ZIGBEE_GATEWAY2(21, "GL-S4型网关"),

    ZIGBEE_GATEWAY3(22, "GL-SL型网关"),

    ZIGBEE_GATEWAY4(24, "GL-SY型网关"),

    ZIGBEE_GATEWAY5(25, "GL-SJ型网关"),

    SCREEN(26, "LED显示屏"),

    LIVE(27, "视频直播流"),

    MEDIA(28, "媒体库"),

    PROGRAM(29, "媒体库"),

    PARK(30, "媒体库"),

    LOOP(31, "回路"),

    POLETYPE(32, "杆体类型"),

    ALARM_SET(33,"报警设置"),

    COVER(34,"智慧井盖"),

    PARK_DEVICE_TYPE(35,"井盖类型"),

    INTEGRATE_TEST(36,"集成测试");

    private Integer deviceType;

    private String deviceName;

    private static final Map<Integer, DeviceTypeEnum> pool = new ConcurrentHashMap<>();

    static {
        for (DeviceTypeEnum enums : DeviceTypeEnum.values()) {
            pool.put(enums.getDeviceType(), enums);
        }
    }

    DeviceTypeEnum(Integer deviceType, String deviceName) {
        this.deviceType = deviceType;
        this.deviceName = deviceName;
    }


    public static DeviceTypeEnum fromType(Integer deviceType, CodeMsg codeMsg) {
        if (deviceType == null) {
            throw new BusinessException(codeMsg.illegalArgumentCode(), codeMsg.illegalArgumentMsg());
        }
        return Optional.ofNullable(pool.get(deviceType))
                .orElseThrow(() -> new BusinessException(codeMsg.illegalArgumentCode(), codeMsg.illegalArgumentMsg()));
    }

    public static DeviceTypeEnum fromType(Integer deviceType) {
        if (deviceType == null) {
            throw new IllegalArgumentException();
        }
        return Optional.ofNullable(pool.get(deviceType))
                .orElseThrow(IllegalArgumentException::new);
    }
}
